/** 默认表单 */
export const DEFAULT_CONFIG = [
  {
    name: 'ID',
    key: 'id',
    required: false,
    editAble: false,
  }, {
    name: '方案名称',
    key: 'name',
    placeholder: '请输入方案名称',
    required: true
  }, {
    name: '营销场景',
    key: 'bizScene',
    uiType: 'select',
    message: '请选择营销场景',
    required: true
  }, {
    name: '机会类型',
    key: 'productType',
    uiType: 'select',
    message: '请选择机会类型',
    required: true
  }, {
    name: '触发类型',
    key: 'triggerType',
    uiType: 'select',
    message: '请选择触发类型',
    required: true,
  }, 
  // {
  //   name: '目标类型',
  //   key: 'triggerOppType',
  //   uiType: 'select',
  //   message: '请选择目标类型',
  //   enum: [{ label: '库', value: 'cap' }, { label: '分发组', value: 'group' }],
  //   required: true,
  //   relyFn: (getFromValue) => {
  //     const value = getFromValue("triggerType")
  //     return value === "opp_transfer"
  //   },
  // },
   {
    name: '目标库列表',
    key: 'triggerId',
    message: '目标库列表',
    required: true,
    placeholder: "用','分割  最多10条",
    relyFn: (getFromValue) => {
      const value = getFromValue("triggerType")
      return value === "opp_transfer"
    },
  }, {
    name: '客户行为',
    key: 'triggerId',
    uiType: 'searchSelect',
    message: '请选择客户行为',
    required: true,
    showSearch: true,
    relyFn: (getFromValue) => {
      const value = getFromValue("triggerType")
      return value === "customer_event"
    },
    searchService: 'listCustomerEvent'//选择搜索的接口
  }, {
    name: '圈人方式',
    key: 'dataType',
    uiType: 'select',
    enum: [
      { label: '机会群组', value: 'group' },
      { label: '商家群组', value: 'shop_group' },
      { label: '批量输入', value: 'input' },
      { label: 'SQL圈人', value: 'sql' }
    ],
    required: true,
    message: '请选择圈人方式',
    allowClear: true,
    relyFn: (getFromValue) => {
      const value = getFromValue("triggerType")
      return value && value !== "old_default"
    },
    requiredFn: (getFromValue) => {
      const value = getFromValue("triggerType")
      return value === 'schedule'
    },
    setEnum: (getFromValue) => {
      const value = getFromValue("triggerType")
      if (value === 'opp_transfer' || value === 'customer_event') {
        return [
          { label: '机会群组', value: 'group' },
          { label: '商家群组', value: 'shop_group' },
          { label: '批量输入', value: 'input' },
        ]
      } else {
        return [
          { label: '机会群组', value: 'group' },
          { label: '商家群组', value: 'shop_group' },
          { label: '批量输入', value: 'input' },
          { label: 'SQL圈人', value: 'sql' }
        ]
      }
    }
  }, {
    name: '群组ID',
    key: 'groupId',
    uiType: 'searchSelect',
    placeholder: '请选择群组ID',
    message: '请选择群组ID',
    required: true,
    relyFn: (getFromValue) => {
      const value = getFromValue("dataType")
      const triggerType = getFromValue("triggerType")
      return triggerType !== "old_default" && value?.includes('group')
    },
    requiredFn: (getFromValue) => {
      const dataType = getFromValue("dataType")
      const triggerType = getFromValue("triggerType")
      return triggerType !== "old_default" && dataType?.includes('group')
    },
    searchService: 'getGroupIdpByTypeDesc'//选择搜索的接口
  }, {
    name: '客户ID列表',
    key: 'ids',
    placeholder: "用','分割  最多一千条",
    required: true,
    relyFn: (getFromValue) => {
      const value = getFromValue("dataType")
      const triggerType = getFromValue("triggerType")
      return triggerType !== "old_default" && value === 'input'
    },
    requiredFn: (getFromValue) => {
      const dataType = getFromValue("dataType")
      const triggerType = getFromValue("triggerType")
      return triggerType !== "old_default" && dataType=='input'
    }
  }, {
    name: 'sql',
    key: 'sql',
    uiType: 'circlePeople',
    required: true,
    rules: {
      whitespace: true,
      validator: (rule, value, callback) => {
        const { required } = rule
        if (!required) return callback()
        if (!value) return callback('请输入sql!')
        if (!value.textContent) return callback('请输入sql')
        const { textContent, legitimate } = value || {}
        if (!legitimate) return callback('sql需要成功校验~')
        if (!textContent || !legitimate) return callback('请保证所有项都已完成~')
        callback()
      }
    },
    relyFn: (getFromValue) => {
      const value = getFromValue("dataType")
      const triggerType = getFromValue("triggerType")
      return triggerType === "schedule" && value === 'sql'
    },
    requiredFn: (getFromValue) => {
      const value = getFromValue("triggerType")
      return value !== 'opp_transfer'
    }
  }, {
    name: '营销频率',
    key: 'scheduleType',
    uiType: 'radio',
    required: true,
    enum: [{ label: '单次调度', value: 'fixed_time' }, { label: '周期调度', value: 'period' }, { label: '自定义调度', value: 'cron' }],
    // defaultValue: 'fixed_time'
    relyFn: (getFromValue) => {
      const triggerType = getFromValue("triggerType")
      return triggerType === "schedule"
    }
  }, {
    name: '营销时间',
    key: 'scheduleTime',
    uiType: 'datePicker',
    required: true,
    relyFn: (getFromValue) => {
      const value = getFromValue("scheduleType")
      const triggerType = getFromValue("triggerType")
      return triggerType === "schedule" && value === 'fixed_time'
    }
  }, {
    name: '触发时间',
    key: 'cronExpression',
    required: true,
    relyFn: (getFromValue) => {
      const triggerType = getFromValue("triggerType")
      const scheduleType = getFromValue("scheduleType")
      return scheduleType === "cron" && triggerType === "schedule"
    },
    tips: <a href="https://aliyuque.antfin.com/bxn3zc/zpkkkg/fqr48pgod5knz8b0?spm=a26311.u_d989b.0.0.47982b4cgVSLpx&singleDoc" target="_blank">查看帮助</a>
  }, {
    name: '循环周期',
    key: 'cycleConfig',
    uiType: 'cycleTime',
    message: '请选择循环周期',
    required: true,
    relyFn: (getFromValue) => {
      const value = getFromValue("scheduleType")
      const triggerType = getFromValue("triggerType")
      return triggerType === "schedule" && value === 'period' ? true : false
    }
  }, {
    name: '期望外呼时间',
    key: 'scheduleTime',
    uiType: 'timePicker',
    required: true,
    relyFn: (getFromValue) => {
      const value = getFromValue("scheduleType")
      const triggerType = getFromValue("triggerType")
      return triggerType === "schedule" && value === 'period' ? true : false
    }
  }, {
    name: '生效状态',
    key: 'isValid',
    required: true,
    uiType: 'select',
    enum: [{ label: '生效', value: 'y' }, { label: '失效', value: 'n' }],
    defaultValue: 'y',
    message: '请选择生效状态'
  }
]

export function debounce(func, wait = 0) {
  if (typeof func !== 'function') {
    throw new TypeError('need a function arguments')
  }
  let timeid = null;
  let result;
  return function () {
    let context = this;
    let args = arguments;
    if (timeid) {
      clearTimeout(timeid);
    }
    timeid = setTimeout(function () {
      result = func.apply(context, args);
    }, wait);
    return result;
  }
}

export function formatGroupData(list, name, id) {
  if (!list || !Array.isArray(list)) {
    return []
  }
  const data = []
  const arr = []
  list.forEach(res => {
    if (!data.includes(res?.[id])) {
      data.push(res?.[id])
      arr.push({ label: res?.[name] + '(' + res?.[id] + ')', value: res?.[id] })
    }
  })
  return arr 
}